package com.bodacn.keypoint;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WorkActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener {
    private String TAG=this.getClass().getName();
    private final int MSG_DetailOver=1001;
    private final int MSG_ErrMessage =1002;
    String STORE_NAME = "Settings";
    String curLoginUserNo,curLoginUser,curServiceUrl="",curServiceDir="",curDepartNo="",curFactory="",curDepart="",CPUID="",WorkSection="";
    String QueryTitle;
    Button btn_find,btn_procSplit,btn_daliyAccount,btn_Close;
    ListView detailList;
    EditText edit_startDate,edit_endDate,edit_orderNo;
    TextView txt_titleInv;
    public static final String DB_Name ="bdmes.db";
    SQLiteDatabase mainDB;
    //全局变量
    private ProgressDialog waitDialog = null;
    Toast curToast;
    JSONArray detailJSON;
    String selOrderNo="",selStyleNo="",selStyleSort="",selWorkDate="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //创建对话框，
        waitDialog=new ProgressDialog(this);
        waitDialog.setTitle("请稍等");
        waitDialog.setIndeterminate(true);
        setContentView(R.layout.activity_work);
        hideBottomUIMenu();
        //获取登陆参数
        Bundle paramBundle = this.getIntent().getExtras();
        curLoginUserNo=paramBundle.getString("LoginUserNo");
        curLoginUser=paramBundle.getString("LoginUser");
        curDepartNo=paramBundle.getString("CurDepartNo");
        curDepart=paramBundle.getString("CurDepart");
        curFactory=paramBundle.getString("CurFactory");
        curServiceUrl=paramBundle.getString("ServiceUrl");
        curServiceDir=paramBundle.getString("ServiceDir");
        CPUID=paramBundle.getString("CPUID");
        WorkSection=paramBundle.getString("WorkSection","SEW");
        QueryTitle=paramBundle.getString("QueryTitle");
        //界面信息
        System.out.println(paramBundle);
        //初始化界面
        btn_find=findViewById(R.id.btn_find);
        btn_find.setOnClickListener(this);
        btn_procSplit=findViewById(R.id.btn_procSplit);
        btn_procSplit.setOnClickListener(this);
        btn_daliyAccount=findViewById(R.id.btn_daliyAccount);
        btn_daliyAccount.setOnClickListener(this);
        btn_Close=findViewById(R.id.btn_Close);
        btn_Close.setOnClickListener(this);
        edit_orderNo=findViewById(R.id.edit_findOrderNo);
        edit_orderNo.setOnClickListener(this);
        txt_titleInv=findViewById(R.id.txt_titleInv);
        txt_titleInv.setText(QueryTitle);
        detailList=findViewById(R.id.detailList);
        detailList.setOnItemClickListener(this);
        //文本框
        edit_startDate = (EditText) findViewById(R.id.edit_startDate);
        edit_startDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {final Calendar calendar = Calendar.getInstance();
                final DatePickerDialog mDialog = new DatePickerDialog(v.getContext(), null, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
                mDialog .setButton(DialogInterface.BUTTON_POSITIVE,getString(R.string.Finish),new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        DatePicker datePicker = mDialog.getDatePicker();
                        int year = datePicker.getYear();
                        int month = datePicker.getMonth();
                        int day = datePicker.getDayOfMonth();
                        calendar.set(year,month,day);
                        edit_startDate.setText(DateFormat.format("yyyy-MM-dd", calendar));
                    }
                });
                mDialog.setButton(DialogInterface.BUTTON_NEGATIVE,getString(R.string.Cancel),new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        System.out.println("cancel~~cancel~~");
                    }
                });
                mDialog.setTitle(getString(R.string.PleaseSelectQueryDate));
                mDialog.show();

            }
        });
        edit_startDate.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                Log.d("edit_startDate","onFocusChange");
                if (hasFocus == true) {
                    hideIM(v);
                }
            }
        });
        edit_endDate = (EditText) findViewById(R.id.edit_endDate);
        edit_endDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Calendar calendar = Calendar.getInstance();
                final DatePickerDialog mDialog = new DatePickerDialog(v.getContext(), null, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
                mDialog .setButton(DialogInterface.BUTTON_POSITIVE,getString(R.string.Finish),new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        DatePicker datePicker = mDialog.getDatePicker();
                        int year = datePicker.getYear();
                        int month = datePicker.getMonth();
                        int day = datePicker.getDayOfMonth();
                        calendar.set(year,month,day);
                        edit_endDate.setText(DateFormat.format("yyyy-MM-dd", calendar));
                    }
                });
                mDialog.setButton(DialogInterface.BUTTON_NEGATIVE,getString(R.string.Cancel),new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        System.out.println("cancel~~cancel~~");
                    }
                });
                mDialog.setTitle(getString(R.string.PleaseSelectQueryDate));
                mDialog.show();
            }
        });
        edit_orderNo = (EditText) findViewById(R.id.edit_findOrderNo);
        edit_orderNo.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                Log.d("edit_orderNo","onFocusChange");
                if (hasFocus == true) {
                    hideIM(v);
                }
            }
        });
        edit_orderNo.setOnKeyListener(new EditText.OnKeyListener(){
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode==66)
                {
                    hideIM(v);
                    btn_find.setFocusable(true);
                    btn_find.setFocusableInTouchMode(true);
                    btn_find.requestFocus();
                    btn_find.requestFocusFromTouch();
                }
                return false;
            }
        });

        //置日期初始值;
        Date curDate = new Date(System.currentTimeMillis());
        String StartDateStr=(String) DateFormat.format("yyyy-MM-dd", new Date(curDate.getTime() -4 * 24 * 60 * 60 * 1000));
        String EndDateStr=(String) DateFormat.format("yyyy-MM-dd", new Date(curDate.getTime()-24 * 60 * 60 * 1000));
        edit_startDate.setText(StartDateStr);
        edit_endDate.setText(EndDateStr);
        //初始提醒;
        mainDB = openOrCreateDatabase(DB_Name, this.MODE_PRIVATE, null);
        curToast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
        Cursor cur= mainDB.rawQuery("Select * from DaliyMa",null);
        if (cur.getCount()>0){
            cur.moveToNext();
            final String tmpStyleNo=cur.getString(cur.getColumnIndex("StyleNo"));
            final String tmpStyleSort=cur.getString(cur.getColumnIndex("StyleSort"));
            final String tmpOrderNo=cur.getString(cur.getColumnIndex("OrderNo"));
            final String tmpWorkDate=cur.getString(cur.getColumnIndex("WorkDate"));
            String tmpDepartNo=cur.getString(cur.getColumnIndex("DepartNo"));
            AlertDialog alertDialog = new AlertDialog.Builder(this)
                    .setTitle("系统提示").setMessage("系统中含有["+tmpWorkDate+"]"+tmpOrderNo+"的未保存的记录，是否需要加载？")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            LoadHis(tmpOrderNo,tmpStyleNo,tmpStyleSort,tmpWorkDate);
                        }
                    }).setNeutralButton("取消",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    btn_find.postDelayed(new Runnable() {
                                        public void run() {
                                            btn_find.performClick();
                                        }
                                    }, 500);
                                }
                            }).create(); // 创建对话框
            alertDialog.show(); // 显示对话框
            return;
        } else {
            btn_find.postDelayed(new Runnable() {
                public void run() {
                    btn_find.performClick();
                }
            }, 500);
        }
    }

    /**
     * 隐藏虚拟按键，并且全屏
     */
    protected void hideBottomUIMenu() {
        //隐藏状态栏和底部虚拟按键
        View decorView = getWindow().getDecorView();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            int flag = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
            decorView.setSystemUiVisibility(flag);
        }
    }
    // 隐藏手机键盘
    private void hideIM(View edt) {
        try {
            InputMethodManager im = (InputMethodManager) getApplicationContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
            IBinder windowToken = edt.getWindowToken();
            if (windowToken != null) {
                im.hideSoftInputFromWindow(windowToken, 0);
            }
        } catch (Exception e) {

        }
    }

    @Override
    protected void onStart() {
        super.onStart();

    }

    private void LoadHis(String paramOrderNo,String paramStyleNo,String paramStyleSort,String paramWorkDate){
        Intent daliyCountActivity= new Intent(this, DaliycountActivity.class);
        //简单参数通过Intent的Extras传递;
        Bundle paramBundle1 = new Bundle();
        paramBundle1.putString("LoginUserNo", curLoginUserNo);
        paramBundle1.putString("LoginUser", curLoginUser);
        paramBundle1.putString("CurFactory", curFactory);
        paramBundle1.putString("CurDepartNo", curDepartNo);
        paramBundle1.putString("CurDepart", curDepart);
        paramBundle1.putString("CurOrderNo", paramOrderNo);
        paramBundle1.putString("CurStyleNo", paramStyleNo);
        paramBundle1.putString("CurStyleSort", paramStyleSort);
        paramBundle1.putString("WorkDate", paramWorkDate);
        paramBundle1.putString("WorkSection", WorkSection);
        paramBundle1.putString("AutoLoadHIS", "Y");
        paramBundle1.putString("CPUID", CPUID);
        daliyCountActivity.putExtras(paramBundle1);
        System.out.println(paramBundle1);
        waitDialog.hide();
        //启动分配界面;
        startActivity(daliyCountActivity);
    }

    @Override
    public void onClick(View v) {
        hideIM(v);
        hideBottomUIMenu();
        switch (v.getId()){
            case R.id.btn_find:
                //查询订单;
                refreshOrderList(curDepartNo,edit_startDate.getText().toString(),edit_endDate.getText().toString(),edit_orderNo.getText().toString(),WorkSection);
                break;
            case R.id.btn_procSplit:
                System.out.println("Click btn_procsplit:"+curDepart);
                Intent procSliptActivity = new Intent(this, ProcActivity.class);
                //简单参数通过Intent的Extras传递;
                Bundle paramBundle = new Bundle();
                paramBundle.putString("LoginUserNo", curLoginUserNo);
                paramBundle.putString("LoginUser", curLoginUser);
                paramBundle.putString("CurFactory", curFactory);
                paramBundle.putString("CurDepartNo", curDepartNo);
                paramBundle.putString("CurDepart", curDepart);
                paramBundle.putString("CurOrderNo", selOrderNo);
                paramBundle.putString("CurStyleNo", selStyleNo);
                paramBundle.putString("CurStyleSort", selStyleSort);
                paramBundle.putString("WorkDate", selWorkDate);
                paramBundle.putString("WorkSection", WorkSection);
                paramBundle.putString("CPUID", CPUID);
                procSliptActivity.putExtras(paramBundle);
                System.out.println();
                System.out.println(paramBundle);
                //启动分配界面;
                startActivity(procSliptActivity);
                break;
            case R.id.btn_daliyAccount:
                if ((selOrderNo.length()>0)&&(selStyleNo.length()>0)&&(selStyleSort.length()>0)) {
                    System.out.println("Click btn_daliyAccount:"+curDepart);
                    Intent daliyCountActivity= new Intent(this, DaliycountActivity.class);
                    //简单参数通过Intent的Extras传递;
                    Bundle paramBundle1 = new Bundle();
                    paramBundle1.putString("LoginUserNo", curLoginUserNo);
                    paramBundle1.putString("LoginUser", curLoginUser);
                    paramBundle1.putString("CurFactory", curFactory);
                    paramBundle1.putString("CurDepartNo", curDepartNo);
                    paramBundle1.putString("CurDepart", curDepart);
                    paramBundle1.putString("CurOrderNo", selOrderNo);
                    paramBundle1.putString("CurStyleNo", selStyleNo);
                    paramBundle1.putString("CurStyleSort", selStyleSort);
                    paramBundle1.putString("WorkDate", selWorkDate);
                    paramBundle1.putString("WorkSection", WorkSection);
                    paramBundle1.putString("CPUID", CPUID);
                    daliyCountActivity.putExtras(paramBundle1);
                    System.out.println(paramBundle1);
                    waitDialog.hide();
                    //启动分配界面;
                    startActivity(daliyCountActivity);
                } else {
                    curToast.setText(getString(R.string.PlsFilterOrderAndSelect));
                    curToast.show();
                }
                break;
            case R.id.btn_Close:
                AlertDialog alertDialog = new AlertDialog.Builder(v.getContext())
                        .setTitle("系统提示").setMessage("你确定要关闭当前窗口？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                finish();
                            }
                        }).setNeutralButton("取消",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        return;
                                    }
                                }).create(); // 创建对话框
                alertDialog.show(); // 显示对话框

                break;
            default:
                break;
        }
    }

    //刷新工序列表;
    private void refreshOrderList(String departParam,String startDateParam,String endDateParam,String orderParam,String workSectionParam)
    {
        selOrderNo="";
        selStyleNo="";
        selStyleSort="";
        selWorkDate="";
        try {
            detailJSON=new JSONArray("[]");
            //显示到orderListGrid中;
            final DetailListAdapter detailListAdapter=new DetailListAdapter(this);
            detailList.setAdapter(detailListAdapter);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        waitDialog.setMessage(getString(R.string.WaitingFromNetGetData));
        String sqlCmd="Exec ScenePAD_DaliyGetOrder @OperType='GetOrder',@DepartmentNo='" + departParam + "',@StartDate='"+startDateParam+"',@EndDate='"+endDateParam+"',@OrderNo='"+orderParam+"',@WorkSection='"+workSectionParam+"',@UserNo='"+curLoginUserNo+"',@CPUID='"+CPUID+"'";
        ReadSQLData(sqlCmd);
    }



    //通过消息来执行处理类程序,不更新UI则不一定需要通过这个;
    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            // TODO Auto-generated method stub
            // 根据消息ID来判断要做的事情
            int msgId = msg.what;
            if (waitDialog.isShowing()) waitDialog.hide();
            switch (msgId) {
                case MSG_DetailOver:
                    //显示到orderListGrid中;
                    final DetailListAdapter detailListAdapter=new DetailListAdapter(getApplicationContext());
                    detailList.setAdapter(detailListAdapter);
                    break;
                case MSG_ErrMessage:
                    Bundle bundle=msg.getData();
                    String SQLExcept=bundle.getString("SQLExcept");
                    String SQLMessage=bundle.getString("SQLMessage");
                    curToast.setText(SQLMessage);
                    curToast.show();
                    break;
                default:
                    break;
            }
        }
    };

    //直接连获取数据，与Handler配套使用;
    private void ReadSQLData ( final String SQLComm){
        waitDialog.show();
        System.out.println("SQLComm:"+SQLComm);
        Runnable run = new Runnable() {
            @Override
            public void run() {
                try {
                    JSONArray jsonArray = DBUtil.QuerySQL2JSON(SQLComm);
                    System.out.println(jsonArray);
                    //往界面转数据;
                    if ((jsonArray != null) && (jsonArray.length() > 0)) {
                        if (jsonArray.optJSONObject(0).has("OperType") == true) {
                            //呼叫信息及库存警告
                            if (jsonArray.optJSONObject(0).getString("OperType").equals("GetOrder") == true) {
                                detailJSON = jsonArray;
                                //显示预排单;
                                Message msg = new Message();
                                msg.what = MSG_DetailOver;
                                handler.removeMessages(msg.what);
                                handler.sendMessage(msg);
                            }
                        }
                        if (jsonArray.optJSONObject(0).has("SQLExcept")){
                            curToast.setText(jsonArray.optJSONObject(0).getString("SQLMessage"));
                            curToast.show();
                            //显示预排单;
                            Message msg = new Message();
                            Bundle bundle=new Bundle();
                            bundle.putString("SQLExcept",jsonArray.optJSONObject(0).getString("SQLExcept"));
                            bundle.putString("SQLMessage",jsonArray.optJSONObject(0).getString("SQLMessage"));
                            msg.setData(bundle);
                            msg.what = MSG_ErrMessage;
                            handler.removeMessages(msg.what);
                            handler.sendMessage(msg);
                        }
                    }else{
                        curToast.setText(jsonArray.optJSONObject(0).getString("SQLMessage"));
                        curToast.show();
                        //显示预排单;
                        Message msg = new Message();
                        Bundle bundle=new Bundle();
                        bundle.putString("SQLExcept","ErrInfo");
                        bundle.putString("SQLMessage","未返回任何数据!");
                        msg.setData(bundle);
                        msg.what = MSG_ErrMessage;
                        handler.removeMessages(msg.what);
                        handler.sendMessage(msg);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

        };
        new Thread(run).start();

    }


    //订单表格适配器;
    public final class DetailListAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        public DetailListAdapter(Context context) {
            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        @Override
        public int getCount() {
            return detailJSON.length();
        }

        @Override
        public Object getItem(int position) {
            try {
                return detailJSON.get(position);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) convertView = inflater.inflate(R.layout.activity_work_item, null, false);
            int orderListWidth=parent.getWidth();
            convertView.setLayoutParams(new ListView.LayoutParams((int)orderListWidth,(int)(45)));
            int textColor=getResources().getColor(R.color.text_black);
            try {
                Log.d("msg","getView()----------------------------------------------------->"+position);
                if (detailJSON.optJSONObject(position).has("ConColor")){
                    textColor=GetColorValue(detailJSON.optJSONObject(position).getString("ConColor"));
                }
                Log.d("msg","getView()----------------------------------------------------->"+position);
                TextView tv = (TextView) convertView.findViewById(R.id.text_WorkDate);// 显示文字
                if (detailJSON.optJSONObject(position).has("WorkDate")==true) tv.setText(detailJSON.optJSONObject(position).get("WorkDate").toString());
                tv.setTextColor(textColor);
                tv = (TextView) convertView.findViewById(R.id.text_RptStyleNo);// 显示文字
                if (detailJSON.optJSONObject(position).has("RptStyleNo")==true) tv.setText(detailJSON.optJSONObject(position).get("RptStyleNo").toString());
                tv.setTextColor(textColor);
                tv = (TextView) convertView.findViewById(R.id.text_OrderNo);// 显示文字
                if (detailJSON.optJSONObject(position).has("OrderNo")==true) tv.setText(detailJSON.optJSONObject(position).get("OrderNo").toString());
                tv.setTextColor(textColor);
                tv = (TextView) convertView.findViewById(R.id.text_StyleNo);// 显示文字
                if (detailJSON.optJSONObject(position).has("StyleNo")==true) tv.setText(detailJSON.optJSONObject(position).get("StyleNo").toString());
                tv.setTextColor(textColor);
                tv = (TextView) convertView.findViewById(R.id.text_StyleSort);// 显示文字
                if (detailJSON.optJSONObject(position).has("StyleSort")==true) tv.setText(detailJSON.optJSONObject(position).get("StyleSort").toString());
                tv.setTextColor(textColor);
                tv = (TextView) convertView.findViewById(R.id.text_Number);// 显示文字
                if (detailJSON.optJSONObject(position).has("Number")==true) tv.setText(detailJSON.optJSONObject(position).get("Number").toString());
                tv.setTextColor(textColor);
                tv = (TextView) convertView.findViewById(R.id.text_ProcArrange);// 显示文字
                if (detailJSON.optJSONObject(position).has("ProcArrange")==true) tv.setText(detailJSON.optJSONObject(position).get("ProcArrange").toString());
                tv.setTextColor(textColor);
                tv = (TextView) convertView.findViewById(R.id.text_DaliyCount);// 显示文字
                if (detailJSON.optJSONObject(position).has("DaliyCount")==true) tv.setText(detailJSON.optJSONObject(position).get("DaliyCount").toString());
                tv.setTextColor(textColor);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            Log.d("msg","DetailListAdapter_position:"+position);
            return convertView;
        }
    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (parent==detailList)
        {
            view.setSelected(true);
            //置选择制单信息;
            try {
                if (detailJSON.optJSONObject(position).has("OrderNo")==true) selOrderNo=detailJSON.getJSONObject(position).getString("OrderNo");
                if (detailJSON.optJSONObject(position).has("StyleNo")==true) selStyleNo=detailJSON.getJSONObject(position).getString("StyleNo");
                if (detailJSON.optJSONObject(position).has("StyleSort")==true) selStyleSort=detailJSON.getJSONObject(position).getString("StyleSort");
                if (detailJSON.optJSONObject(position).has("WorkDate")==true) selWorkDate=detailJSON.getJSONObject(position).getString("WorkDate");
                if (detailJSON.optJSONObject(position).has("RptStyleNo")==true) selOrderNo=detailJSON.getJSONObject(position).getString("RptStyleNo");
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

    }

        //从字符串转颜色
        private int GetColorValue(String paramColor){
            int PenColor= Color.WHITE;
            String StrPenColor=paramColor;
            if ((StrPenColor.indexOf("#")==0)&&(StrPenColor.length()==7)){
                int testColor=Integer.parseInt(StrPenColor.substring(1),16);
                int red = (testColor & 0xff0000) >> 16;
                int green = (testColor & 0x00ff00) >> 8;
                int blue = (testColor & 0x0000ff);
                PenColor=Color.rgb(red, green, blue);
            }else {
                if (isNumer(StrPenColor)) {
                    int testColor = Integer.parseInt(StrPenColor);
                    int red = (testColor & 0xff0000) >> 16;
                    int green = (testColor & 0x00ff00) >> 8;
                    int blue = (testColor & 0x0000ff);
                    PenColor = Color.rgb(red, green, blue);
                } else {
                    if (StrPenColor.equals("Black") == true)
                        PenColor = Color.BLACK;
                    if (StrPenColor.equals("Blue") == true)
                        PenColor = Color.BLUE;
                    if (StrPenColor.equals("Yellow") == true)
                        PenColor = Color.YELLOW;
                    if (StrPenColor.equals("Red") == true)
                        PenColor = Color.RED;
                    if (StrPenColor.equals("Green") == true)
                        PenColor = Color.GREEN;
                    if (StrPenColor.equals("Gray") == true)
                        PenColor = Color.LTGRAY;
                    if (StrPenColor.equals("White") == true)
                        PenColor = Color.WHITE;
                }
            }
            return PenColor;
        }

        //判断是否为数字
        public static boolean isNumer(String str) {
            Pattern pattern = Pattern.compile("-?[0-9]+\\.?[0-9]+");
            Matcher isNum = pattern.matcher(str);
            if (!isNum.matches()) {
                return false;
            }
            return true;
        }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (waitDialog!=null) waitDialog.dismiss();
    }
}
